package com.jee.ssm.modules.posUser.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSONObject;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.common.utils.MD5;
import com.jee.ssm.common.utils.PropertiesLoader;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.model.*;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.modules.posUser.services.PosUserService;
import com.jee.ssm.modules.posuserBerth.services.PosuserBerthService;
import com.jee.ssm.modules.ssm.controller.AdminBaseController;
import com.jee.ssm.modules.ssm.services.AccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 手持机用户管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/posUser")
public class PosUserController extends AdminBaseController<PosUser> {


    /**
     * @Description 获取全部路侧停车场
     * @author yanzengbao
     * @date 2019/8/8 上午9:50
     */
    public void getParkList(ModelMap model){
        try {
            Props props = new Props("config/project.properties");
            String serverUrl = props.getProperty("server.url");
            String method = props.getStr("getRoadsideParking");
            String result = HttpRequest.get(serverUrl + method).execute().body();
            List<Park> parkList = JSONObject.parseArray(result,Park.class);
            model.put("parkList",parkList);
        }catch (Exception e){
            //e.printStackTrace();
            System.out.println("服务器未链接！");
        }
    }


    /**
     * 进入手持机用户添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("posUser:add")
    public String add(ModelMap model){

        getParkList(model);
        model.put("longId", UUIDFactory.getStringId());
        return "manager/posUser/add";
    }


    /**
     * 进入手持机用户编辑页面
     * @param model 返回手持机用户的容器
     * @param id 手持机用户id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("posUser:edit")
    public String edit(ModelMap model, @RequestParam String id) {
        getParkList(model);
        model.put("data",posUserService.selectById(id));
        return "manager/posUser/edit";
    }


    /**
     * 手持机用户添加
     * @param posUser 带id的手持机用户对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("posUser:add")
    @AdminControllerLog(description = "添加手持机用户" )
    public Tip save(PosUser posUser)  {

        Account byUserName = accountService.findByUserName(posUser.getPosUserName());
        if(byUserName != null) return new Tip(1,"该用户名已存在，请重新输入！");
        try {
            posUser.setCreateTime(new Date());
            posUser.setOnlineStatus(0);
            posUserService.insert(posUser);
            Account account = new Account();
            account.setPassword(MD5.md5(posUser.getPassword()));
            account.setUserName(posUser.getPosUserName());
            account.setStatus(1);
            account.setType(4);
            account.setId(UUIDFactory.getStringId());
            account.setInfoId(posUser.getId());
            accountService.insert(account);
            return new Tip("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"添加失败！");
        }
    }


    /**
     * 根据 id 修改手持机用户
     * @param posUser 带id的手持机用户对象
     * @return 成功状 态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("posUser:edit")
    @AdminControllerLog(description = "修改手持机用户" )
    public Tip update(PosUser posUser) {

        try {
//            String parkName = parkService.selectById(posUser.getParkId()).getName();
//            posUser.setParkName(parkName);
            posUser.setCreateTime(new Date());
            posUserService.updateById(posUser);
            return new Tip("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除手持机用户
     * @param id 手持机用户id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("posUser:delete")
    @AdminControllerLog(description = "删除手持机用户" )
    public Tip delete(@RequestParam String id) {

        try {
            posUserService.deleteById(id);
            return new Tip("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 手持机用户id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("posUser:delete")
    @AdminControllerLog(description = "批量删除手持机用户" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            posUserService.deleteByIds(ids);
            return new Tip("批量删除成功！");
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找手持机用户
     * @param id 手持机用户id
     * @return 手持机用户对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("posUser:list")
    public PosUser find(@RequestParam String id) {

        return posUserService.selectById(id);
    }


    /**
     * 获取手持机用户列表 获取全部 不分页
     * @param request 请求参数
     * @return 手持机用户列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("posUser:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取手持机用户列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 手持机用户列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("posUser:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
        getParkList(modelMap);
        modelMap.put("pageInfo",posUserService.list(new ParamMap(request),page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/posUser/list";
    }


    /**
     * 收费员管理车位
     * @return
     */
    @RequestMapping("/manageBerth")
    public  String manageBerth(String id,ModelMap modelMap){
        PosUser posUser = posUserService.selectById(id);
        if(posUser !=null){
            String parkId = posUser.getParkId();// 　收费员管理的停车场信息
            PropertiesLoader propertiesLoader = new PropertiesLoader("config/project.properties");
            String serverUrl = propertiesLoader.getProperty("server.url");
            String method = propertiesLoader.getProperty("getAllBerthByParkId");
            String result = HttpRequest.get(serverUrl + method).form("parkId",parkId).execute().body();
            List<Berth> berths = JSONObject.parseArray(result,Berth.class);
            for(Berth berth:berths){
                Map map = new HashMap<>();
                map.put("berthId",berth.getId());
                PosuserBerth posuserBerth = posuserBerthService.selectByParam(map);
                if(posuserBerth != null){
                        String uId = posuserBerth.getUserId();
                        if(!uId.equals(id)){ //当前车位已经被其他的收费管理员绑定
                            berth.setDisabled(true);
                        }else{// 车位被自己绑定
                            berth.setSelected(true);
                        }
                }
            }
            modelMap.put("berths",berths);
            modelMap.put("parkName",posUser.getParkName());
        }
        return  "/manager/posUser/manage_berth";
    }
    //---------------------------- property -------------------------------

    @Resource
    private PosUserService posUserService;
//    @Resource
//    private ParkService parkService;
    @Resource
    private AccountService accountService;

    @Resource
    private PosuserBerthService posuserBerthService;

}
